﻿using System.IO;
using System;
using System.Collections.Generic;
using System.Text;

namespace xReplace
{
    public class RenameHelper
    {

        public static string PerformRename(string filename, string rules)
        {
            return PerformRename(filename, rules, null);
        }

        public static string PerformRename(string filename, string rules, Content pipeline)
        {
            string dir = Path.GetDirectoryName(filename);
            string name = Path.GetFileNameWithoutExtension(filename);
            string extension = Path.GetExtension(filename);
            string[] functions = rules.Split(',');



            for (int i = 0; i < functions.Length; i++)
            {
                functions[i] = functions[i].Replace("^:", "${COLON}");
                string[] param = functions[i].Split(':');
                param[0] = param[0].ToLower().Trim();

                if (param[0] == "@drop")
                {
                    if(param.Length != 2) throw new Exception("Syntax error: incorrect number of parameters for " + functions[i]);
                    string frag = GetParam(param[1], pipeline);
                    int targetIndex = name.IndexOf(frag);
                    if (targetIndex > -1)
                    {
                        name = name.Remove(targetIndex);
                    }
                }
                else if (param[0] == "@replace")
                {
                    if (param.Length != 3) throw new Exception("Syntax error: incorrect number of parameters for " + functions[i]);
                    name = name.Replace(GetParam(param[1], pipeline), GetParam(param[2], pipeline));
                    dir = dir.Replace(GetParam(param[1], pipeline), GetParam(param[2], pipeline));
                }
                else if (param[0] == "@insert")
                {
                    if (param.Length != 2) throw new Exception("Syntax error: incorrect number of parameters for " + functions[i]);
                    name = GetParam(param[1], pipeline) + name;
                }
                else if (param[0] == "@append")
                {
                    if (param.Length != 2) throw new Exception("Syntax error: incorrect number of parameters for " + functions[i]);
                    name = name + GetParam(param[1], pipeline);
                }
                if (param[0] == "@remove")
                {
                    if (param.Length != 2) throw new Exception("Syntax error: incorrect number of parameters for " + functions[i]);
                    name = name.Replace(GetParam(param[1], pipeline), "");
                    dir = dir.Replace(GetParam(param[1], pipeline), "");
                }
                if (param[0] == "@extension")
                {
                    if (param.Length != 3) throw new Exception("Syntax error: incorrect number of parameters for " + functions[i]);
                    if (extension.Contains(GetParam(param[1], pipeline))) extension = GetParam(param[2], pipeline);
                }
            }
            if (!extension.StartsWith(".")) extension = "." + extension;
            return Path.Combine(dir, name + extension);
        }

        private static string GetParam(string value, Content pipeline)
        {
            if (pipeline != null)
            {
                Dictionary<string, string>.Enumerator e = pipeline.GetEnumerator();
                while (e.MoveNext()) value = value.Replace("${" + e.Current.Key + "}", e.Current.Value);
            }
            return value.Replace("${COLON}", ":");
        }
    }
}
